package edu.vt.ece_3574.amber_alert.membership;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import edu.vt.ece_3574.amber_alert.LoginActivity;
import edu.vt.ece_3574.amber_alert.R;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;


public class AccountCreationActivity extends Activity {
	private EditText username;
	
	/** Called when the activity is first created. */
	private String name;
	private String pass1;
	private String pass2;

	private String invalidPassword_ = "Invalid password. Either the passwords do not match or it contains a ':' character.";
	private String fillOutField_ = "Please fill out all fields";
	private String success_ = "The primary account was created successfully with data: ";

	@Override 
	public void onBackPressed() { 
	   return; 
	} 

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_creation_screen);

		Button createAccountButton = (Button) findViewById(R.id.accbutton);
		Button returntoLogin = (Button) findViewById(R.id.returncreateAccount);

		createAccountButton.setOnClickListener(onButtonClick);
		returntoLogin.setOnClickListener(onReturnClick);
	}

	private View.OnClickListener onButtonClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			username = (EditText) findViewById(R.id.usernameCreationField);
			EditText password1 = (EditText) findViewById(R.id.passwordCreationField);
			EditText password2 = (EditText) findViewById(R.id.repassCreationField);

			if (username != null) {
				pass1 = password1.getText().toString();
				pass2 = password2.getText().toString();
				name = username.getText().toString();

				if ((username.getText().toString().contains(":")) || (username.getText().toString().contains(";")) || (username.getText().toString().contains(" ")) ||
						(pass1.contains(":")) || (((EditText)findViewById(R.id.phoneNumberEditText)).getText().toString().contains(":"))) {
					
					Context context = getApplicationContext();
					CharSequence text = "Invalid information. An invalid character was specified in one of the fields.";
					int duration = Toast.LENGTH_LONG;
					Toast toast = Toast.makeText(context, text, duration);
					toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
					toast.show();
				}
				else if (username.getText().toString().equals("") || pass1.equals("")) {
					Context context = getApplicationContext();
					CharSequence text = "One of the fields was left blank.";
					int duration = Toast.LENGTH_LONG;
					Toast toast = Toast.makeText(context, text, duration);
					toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
					toast.show();
				}
				else if (DatabaseHandler.get().determineIfUserExistsOnServer(username.getText().toString())) {
					Context context = getApplicationContext();
					CharSequence text = "That username has already been taken.";
					int duration = Toast.LENGTH_LONG;
					Toast toast = Toast.makeText(context, text, duration);
					toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
					toast.show();
				}
				else {
					if (pass1.equals(pass2)) {
						createAccount();
						Context context = getApplicationContext();
						CharSequence text = success_ + " Username: " + name + ", Password: " + pass1;
						int duration = Toast.LENGTH_LONG;

						Toast toast = Toast.makeText(context, text, duration);
						toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
						toast.show();
					} 
					else {
						Context context = getApplicationContext();
						CharSequence text = (invalidPassword_);
						int duration = Toast.LENGTH_LONG;

						Toast toast = Toast.makeText(context, text, duration);
						toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
						toast.show();
					}
				}
			} 
			else {
				Context context = getApplicationContext();
				CharSequence text = fillOutField_ + "," + name + "," + pass1 + "," + pass2;
				int duration = Toast.LENGTH_LONG;

				Toast toast = Toast.makeText(context, text, duration);
				toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
				toast.show();
			}
		}
	};

	private View.OnClickListener onReturnClick = new View.OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(AccountCreationActivity.this, LoginActivity.class);
			startActivity(i);
			AccountCreationActivity.this.finish();
		}
	};

	public void createAccount() {
		String phoneNumber = ((EditText) findViewById(R.id.phoneNumberEditText)).getText().toString();
		String name = username.getText().toString();
		
		String type;
		RadioButton childRadioButton = (RadioButton)findViewById(R.id.childRadioButton);
		if (childRadioButton.isChecked()) {
			type = "Member";
		}
		else {
			type = "Primary";
		}
		
		Account newAccount = new Account(phoneNumber, name, type, pass1);
		DatabaseHandler.get().addAccountToServer(newAccount);

		//Return to the login screen after creating an account
		Intent i = new Intent(AccountCreationActivity.this, LoginActivity.class);
		startActivity(i);
		AccountCreationActivity.this.finish();
	}

}